package com.hjm.ui.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjm.ui.R;

/**
 * 上面图片，下面文字
 */
public class ImageLabel extends LinearLayout {
    private ImageView imageView;
    private TextView textView;
    public ImageLabel(Context context) {
        super(context);
        init();
    }

    public ImageLabel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageLabel);
        String labelString = a.getString(R.styleable.ImageLabel_labelText);
        Drawable labelDrawable = a.getDrawable(R.styleable.ImageLabel_labelImg);
        int height = (int) a.getDimension(R.styleable.ImageLabel_labelImgHeight, 24*getResources().getDisplayMetrics().density);
        int labelStyleResourceId = a.getResourceId(R.styleable.ImageLabel_labelStyle, R.style.ImageTextLabelStyle);
        if(labelDrawable!=null){
            imageView.setImageDrawable(labelDrawable);
        }
        if(labelString!=null){
            textView.setText(labelString);
        }
        int dp_24 = (int) (24*getResources().getDisplayMetrics().density);
        if(height!=dp_24){
            imageView.getLayoutParams().height = height;
        }
        if(labelStyleResourceId!=R.style.ImageTextLabelStyle){
            textView.setTextAppearance(labelStyleResourceId);
        }
    }

    public ImageLabel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        // 在这里添加你的布局和逻辑
        LayoutInflater.from(getContext()).inflate(R.layout.layout_image_label, this, true);
        imageView = findViewById(R.id.image_label_img);
        textView = findViewById(R.id.image_label_text);
    }

    public void setOnclickListener(OnClickListener listener){
        LinearLayout layout = findViewById(R.id.image_label_layout);
        layout.setOnClickListener(listener);
    }
}
